<?php

/**
 * BolaoNews form.
 *
 * @package    bolao-aabb
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class BolaoNewsForm extends BaseBolaoNewsForm
{
  public function configure()
  {
    unset($this['created_at']);

    $this->widgetSchema['creator_id'] = new sfWidgetFormInputHidden();

    $this->widgetSchema['summary'] = new sfWidgetFormInputText(array('label' => 'T&iacute;tulo'), array('class' => 'large'));
    $this->validatorSchema['summary']->setMessage('max_length', 'O n&uacute;mero m&aacute;ximo de caracteres é %max_length%.');

    $this->widgetSchema['description'] = new sfWidgetFormTextareaTinyMCE(array('label' => 'Descri&ccedil;&atilde;o'));
    $this->validatorSchema['description']->setMessage('max_length', 'O n&uacute;mero m&aacute;ximo de caracteres é %max_length%.');

    $this->widgetSchema['category_id'] = new sfWidgetFormPropelChoice(array(
      'label' => 'Categoria',
      'model' => 'BolaoCategory',
      'order_by' => array('Name', 'desc'),
      'multiple' => false,
      'expanded' => true
    ));
    $this->validatorSchema['category_id'] = new sfValidatorPropelChoice(array(
      'model' => 'BolaoCategory',
      'multiple' => false
    ));

    $this['subcategory_id']->getWidget()->setLabel('Subcategoria');

    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'check_subcategory'))));

    $this->widgetSchema['send_email'] = new sfWidgetFormInputCheckbox(array(
      'label' => 'Enviar e-mail?'
    ));
    $this->validatorSchema['send_email'] = new sfValidatorBoolean();
  }
  
  function check_subcategory($validator, $values)
  {
    if (empty($values['subcategory_id'])) return $values;
    
    $subcategory = BolaoSubcategoryPeer::retrieveByPK($values['subcategory_id']);
    if ($subcategory == NULL || $subcategory->getCategoryId() != $values['category_id'])
    {
      $error = new sfValidatorError($validator, 'Esta subcategoria n&atilde;o &eacute; parte da categoria escolhida.');
 
      // throw an error bound to the subcategory field
      throw new sfValidatorErrorSchema($validator, array('subcategory_id' => $error));
    }

    return $values;
  }
}
